package com.ruoyi.project.WebMoudle.shopCarouse.controller;

import java.io.IOException;
import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.project.WebMoudle.ShopSuser.domain.ShopSuser;
import com.ruoyi.project.WebMoudle.ShopSuser.service.IShopSuserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.WebMoudle.shopCarouse.domain.ShopCarousel;
import com.ruoyi.project.WebMoudle.shopCarouse.service.IShopCarouselService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 店铺轮播Controller
 * 
 * @author ruoyi
 * @date 2020-06-29
 */
@Controller
@RequestMapping("/shopCarouse/shopCarouse")
public class ShopCarouselController extends BaseController
{
    private String prefix = "shopCarouse/shopCarouse";

    @Autowired
    private IShopCarouselService shopCarouselService;
    @Autowired
    private IShopSuserService shopSuserService;



    @RequiresPermissions("shopCarouse:shopCarouse:view")
    @GetMapping()
    public String shopCarouse()
    {
        return prefix + "/shopCarouse";
    }

    /**
     * 查询店铺轮播列表
     */
    @RequiresPermissions("shopCarouse:shopCarouse:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ShopCarousel shopCarousel)
    {
        startPage();
       /* Long userId = getSysUser().getUserId();
        ShopSuser shopSuser = shopSuserService.selectOneShopId(userId);
        if (StringUtils.isNotNull(shopSuser)){
            shopCarousel.setShopId(shopSuser.getShopId());
        }*/
        List<ShopCarousel> list = shopCarouselService.selectShopCarouselList(shopCarousel);
        return getDataTable(list);
    }

    /**
     * 导出店铺轮播列表
     */
    @RequiresPermissions("shopCarouse:shopCarouse:export")
    @Log(title = "店铺轮播", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ShopCarousel shopCarousel)
    {
        List<ShopCarousel> list = shopCarouselService.selectShopCarouselList(shopCarousel);
        ExcelUtil<ShopCarousel> util = new ExcelUtil<ShopCarousel>(ShopCarousel.class);
        return util.exportExcel(list, "shopCarouse");
    }

    /**
     * 新增店铺轮播
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存店铺轮播
     */
    @RequiresPermissions("shopCarouse:shopCarouse:add")
    @Log(title = "店铺轮播", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam("imgFile") MultipartFile imgFile, ShopCarousel shopCarousel)
    {
        try {
            //上传图片
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String imgName = FileUploadUtils.upload(filePath, imgFile);
            shopCarousel.setImgUrl(imgName);
            Long userId = getSysUser().getUserId();
            ShopSuser shopSuser = shopSuserService.selectOneShopId(userId);
            shopCarousel.setShopId(shopSuser.getShopId());
        } catch (IOException e) {
            e.printStackTrace();
        }

        return toAjax(shopCarouselService.insertShopCarousel(shopCarousel));
    }

    /**
     * 修改店铺轮播
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ShopCarousel shopCarousel = shopCarouselService.selectShopCarouselById(id);
        mmap.put("shopCarousel", shopCarousel);
        return prefix + "/edit";
    }

    /**
     * 修改保存店铺轮播
     */
    @RequiresPermissions("shopCarouse:shopCarouse:edit")
    @Log(title = "店铺轮播", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ShopCarousel shopCarousel)
    {
        return toAjax(shopCarouselService.updateShopCarousel(shopCarousel));
    }

    /**
     * 删除店铺轮播
     */
    @RequiresPermissions("shopCarouse:shopCarouse:remove")
    @Log(title = "店铺轮播", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(shopCarouselService.deleteShopCarouselByIds(ids));
    }

    @RequiresPermissions("shopCarouse:shopCarouse:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(ShopCarousel shopCarousel)
    {
        return toAjax(shopCarouselService.updateShopCarousel(shopCarousel));
    }
}
